package com.example.ck.controller;

import com.example.ck.service.SmslogService;
import com.example.ck.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sms")
@CrossOrigin
public class SmslogController {

    @Autowired
    private SmslogService smslogService;

    @GetMapping("/getcode")
    public ResultVo getCode(String phone) {
        int code = smslogService.sendCode(phone);
        ResultVo rv = new ResultVo<>();
        if (code != 0) {
            rv.setCode(200);
            rv.setSuccess(true);
            rv.setMessage("发送成功");
            rv.setData(code);
        } else {
            rv.setCode(201);
            rv.setSuccess(false);
            rv.setMessage("发送失败");
            rv.setData(code);
        }
        return rv;
    }

    // 通知用户创建成功
    @PostMapping("/create")
    public ResultVo getCode(String phone,@RequestBody String name) {
        int success = smslogService.createSuccess(phone, name);
        ResultVo rv = new ResultVo<>();
        if (success != 0) {
            rv.setCode(200);
            rv.setSuccess(true);
            rv.setMessage("发送成功");
            rv.setData(success);
        } else {
            rv.setCode(201);
            rv.setSuccess(false);
            rv.setMessage("发送失败");
            rv.setData(success);
        }
        return rv;
    }
}
